from .models import owgameserver
from django.dispatch import receiver, Signal
from django.db.models.signals import post_delete, post_save, pre_save, pre_delete


@receiver(pre_delete, sender=owgameserver)
def del_ansible_host(sender, instance, **kwargs):
    delserver = owgameserver.objects.filter(pk=instance.serverid)
    print('删除%s,%s' % (delserver[0].servername, delserver[0].serverhost))
    print('删除文件中的列表')


# @receiver(post_save, sender=owgameserver)
def save_ansible_host(sender, instance, created, **kwargs):
    print("after save")
    if created:
        print(instance)
    addserver = owgameserver.objects.filter(pk=instance.serverid)
    print('添加:%s,%s' % (addserver[0].servername, addserver[0].serverhost))
    print('保存内容')


post_save.connect(save_ansible_host, sender=owgameserver)

from django.core.signals import request_finished


def my_callback(sender, **kwargs):
    print("Request finished!")


request_finished.connect(my_callback)

# -------------------------------


import django.dispatch

# 1 定义信号
pizza_done = django.dispatch.Signal(providing_args=["name", "ip", "supervisor_host"])


# 2 发送信号

class PizzaStore(object):
    # ...

    def send_pizza(self, toppings, size):
        pizza_done.send(sender=self.__class__, toppings=toppings, size=size)
        # ...


# 接收信号
def my_signal_call_back(*args, **kwargs):
    print("args", args)
    print("kwrgs", kwargs)


pizza_done.connect(receiver=my_signal_call_back, sender=PizzaStore)
